package com.peanut.baby.mvp.mydata;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.baby.model.Record;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.record.RecordDisplayActivity;
import com.peanut.devlibrary.widget.pullrecycle.BaseRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/8/28.
 */

public class ZhenliaoListFragment extends BaseMvpFragment implements RecordListContract.View,
        PullRecyclerView.OnRecyclerRefreshListener,BaseRecyclerAdapter.OnRecyclerItemClickListener {
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecycler;
    Unbinder unbinder;

    private int pageNo = 1;
    private int pageSize = 10;
    private RecordRecyclerAdapter adapter;
    private List<Record> records;
    private ZhenliaoListPresenter presenter;

    public static ZhenliaoListFragment newInstance(){
        return new ZhenliaoListFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_recycler, null);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void initData() {
        presenter = new ZhenliaoListPresenter(this,getActivity());
        records = new ArrayList<>();
        adapter = new RecordRecyclerAdapter(getActivity(),records);

        pullRecycler.enablePullRefresh(true);
        pullRecycler.enableLoadMore(true);
        pullRecycler.setOnRecyclerRefreshListener(this);
        pullRecycler.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecycler.setAdapter(adapter);

        adapter.setOnRecyclerItemClickListener(this);

        pullRecycler.postRefreshing();

    }

    @Override
    public void onListGet(List<Record> _records) {
        stopRefreshOrLoading();
        if(pageNo == 1){
            records.clear();
        }
        if(_records != null && !_records.isEmpty()){
            records.addAll(_records);
        }
        adapter.notifyDataSetChanged();
        pullRecycler.enableLoadMore(_records != null && _records.size() >= pageSize);
    }

    @Override
    public void onGetListFailed(String reason) {
        stopRefreshOrLoading();
        showToast(reason);
        pullRecycler.enableLoadMore(false);
    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getRecordList(pageNo,pageSize);
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        presenter.getRecordList(pageNo,pageSize);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void stopRefreshOrLoading(){
        pullRecycler.stopLoadMore();
        pullRecycler.stopRefresh();
    }

    @Override
    public void onItemClick(View view, int position) {
        RecordDisplayActivity.start(getActivity(),records.get(position));
    }
}
